<template>
  <div>
    <el-form :model="tableData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-card class="box-card">
        <span>基本信息</span>
        <div class="text item">
          <el-form-item label="订单号">
            <el-input v-model="tableData.order.orderNumber" disabled></el-input>
          </el-form-item>
          <el-form-item label="申请时间">
            <el-input :value="getDate(tableData.order.addTime)" disabled></el-input>
          </el-form-item>

          <el-form-item label="订单状态">
            <el-input v-model="tableData.order.statusString" disabled></el-input>
          </el-form-item>
          <el-form-item label="汽车总数量">
            <el-input v-model="tableData.order.carSumNumber" disabled></el-input>
          </el-form-item>
          <el-form-item label="融资经销商">
            <el-input v-model="tableData.order.dealerName" disabled></el-input>
          </el-form-item>
          <el-form-item label="融资总金额(元)">
            <el-input :value="tableData.order.mortgageSumMoney| toFixed2" disabled></el-input>
             <span class="convertCurrency">{{tableData.order.mortgageSumMoney|convertCurrency}}</span>
          </el-form-item>
          <el-form-item label="初始融资周期">
            <el-input v-model="tableData.order.description" disabled></el-input>
          </el-form-item>
          <el-form-item label="初始融资利率">
            <el-input v-model="tableData.order.mortgageInterestRate" disabled></el-input>
          </el-form-item>
          <el-form-item label="业务员">
            <el-input v-model="tableData.order.businessName" disabled></el-input>
          </el-form-item>

          <el-form-item label="收款账户名" prop='order.accountName'>
            <el-input v-model="tableData.order.accountName" :disabled='dialogSendData.isAdd'></el-input>
          </el-form-item>
          <el-form-item label="收款账号"  prop='order.accountNumber'>
            <el-input v-model="tableData.order.accountNumber" :disabled='dialogSendData.isAdd'></el-input>
          </el-form-item>
          <el-form-item label="开户行"  prop='order.openningBank'>
            <el-input v-model="tableData.order.openningBank" :disabled='dialogSendData.isAdd'></el-input>
          </el-form-item>
        </div>
      </el-card>
      <br>
      <br>
      <el-card v-if="this.buttonValue=='child编辑'">
        <span>付款信息</span>
        <el-form-item label="付款凭证">
          <qiniu :qiniuImage=" tableData.detail.payPics" :packJson="packJson" :id=" this.tableData.detail.payPics.id" ></qiniu>
        </el-form-item>
        <el-form-item label="付款金额(元)" >
          <el-input v-model="tableData.detail.payMoney" :disabled='dialogSendData.isAdd'></el-input>
        </el-form-item>
        <el-form-item label="付款备注" >
          <el-input v-model="tableData.detail.remark" :maxlength='100' :disabled='dialogSendData.isAdd'></el-input>
        </el-form-item>
        <el-form-item label="经手人">
          <el-input v-model="tableData.detail.handling" :disabled='dialogSendData.isAdd'></el-input>
        </el-form-item>
        <el-form-item label="付款时间" >
          <el-date-picker  :editable="false" v-model="tableData.detail.payDate" type="datetime" :picker-options="endDateOpt"  placeholder="选择日期时间">          
          </el-date-picker>
        </el-form-item>
        <el-form-item label="银行流水编号" >
          <el-input v-model="tableData.detail.bankTransaction" :disabled='dialogSendData.isAdd'></el-input>
        </el-form-item>
      </el-card>
      <el-card v-if="this.buttonValue=='添加'" >
        <span>付款信息</span>
        <el-form-item label="付款凭证">
          <qiniu v-if="tableData.detail?false:true"  :qiniuImage="qiniuImage" :packJson="packJson" :id=" qiniuImage.id" :canDelete="dialogSendData.isAdd?false:true"></qiniu>
          
          <qiniu v-else :qiniuImage="tableData.detail.payPics" :packJson="packJson" :id="  this.tableData.detail.payPics.id" :canDelete="dialogSendData.isAdd?false:true"></qiniu>
        </el-form-item>
        <el-form-item label="付款金额 (元)" prop='money'  >
          <el-input v-model="tableData.money" :disabled='dialogSendData.isAdd' :maxlength='12'></el-input>
          <span class="convertCurrency">{{tableData.money|convertCurrency}}</span>
        </el-form-item>
        <el-form-item label="付款备注" >
          <el-input v-model="tableData.remark" :disabled='dialogSendData.isAdd' :maxlength='100' type='textarea'></el-input>
        </el-form-item>
        <el-form-item label="经手人" prop='functionary'>
          <el-input v-model="tableData.functionary" :disabled='dialogSendData.isAdd' :maxlength='100'></el-input>
        </el-form-item>
        <el-form-item label="付款时间" prop='timer' >
          <el-date-picker :editable="false" v-model="tableData.timer" :disabled='dialogSendData.isAdd' type="datetime"  :picker-options="endDateOpt"  placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="银行流水编号"  >
          <el-input v-model="tableData.number" :disabled='dialogSendData.isAdd' :maxlength='100' type='textarea'></el-input>
        </el-form-item>
      </el-card>
      <el-card v-if="this.buttonValue=='child查看'">
        <span>付款信息</span>
        <el-form-item label="付款凭证">
          <qiniu :qiniuImage=" tableData.detail.payPics" :packJson="packJson" :id=" this.tableData.detail.payPics.id" ></qiniu>
        </el-form-item>
        <el-form-item label="付款金额(元)" >
          <el-input :value="tableData.detail.payMoney| toFixed2" disabled></el-input>
            <span class="convertCurrency">{{tableData.detail.payMoney|convertCurrency}}</span>
        </el-form-item>
        <el-form-item label="付款备注" >
          <el-input v-model="tableData.detail.remark" disabled></el-input>
        </el-form-item>
        <el-form-item label="经手人">
          <el-input v-model="tableData.detail.handling" disabled></el-input>
        </el-form-item>
        <el-form-item label="付款时间" >
          <el-date-picker  :editable="false" v-model="tableData.detail.payDate" type="datetime" disabled placeholder="选择日期时间">          
          </el-date-picker>
        </el-form-item>
        <el-form-item label="银行流水编号" >
          <el-input v-model="tableData.detail.bankTransaction" disabled></el-input>
        </el-form-item>
        <!-- <el-form-item label="付款单审核备注">
          <el-input v-model="tableData.detail.checkRemark" disabled></el-input>
        </el-form-item>
        <el-form-item label="付款单审核人">
          <el-input v-model="tableData.detail.checkPerson" disabled></el-input>
        </el-form-item>
        <el-form-item label="付款单审核时间">
          <el-input :value="getDate(tableData.detail.checkTime)" disabled></el-input>
        </el-form-item> -->
     
      </el-card>
      <el-card v-if="this.buttonValue!=='添加'">
         <el-form-item label="付款单审核备注">
          <el-input v-model="tableData.detail.checkRemark" :maxlength='100' :disabled="this.buttonValue=='child查看'||this.buttonValue=='添加'||this.buttonValue=='child编辑'"></el-input>
        </el-form-item>
      
          <el-form-item label="付款单审核人" >
            <el-input v-model="tableData.detail.checkPerson" disabled></el-input>
          </el-form-item>
          <el-form-item label="付款单审核时间">
            <el-input :value="getDate(tableData.detail.checkTime)" disabled></el-input>
          </el-form-item>
      </el-card>
      <el-form-item class='align'>
        <el-button  type="primary"  @click="resetForm('ruleForm')">返回</el-button>
        <el-button type="success" @click="submitForm('ruleForm')" v-if='dialogSendData.isAdd?false:true'>提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import qiniu from '@/views/qiniu/upload';
  import {
    require,
    prop,
    decimalTwo,
    decimal
  } from '@/utils/rules.js' //表单验证规则
  import {
    timestampToTime,
    formatTimer,
  } from '@/utils/index.js'

  import agentHttp from '@/utils/agentHttp.js'


  export default {
    data() {
      return {
        tableData: {
          money: '',
          remark: '',
          functionary: '',
          timer: '',
          number: '',
        },
        rules: {
          'order.accountName':[require("收款账户名")],
          'order.accountNumber':[require("收款账号"),{ validator: decimal, trigger: "blur"}],
          'order.openningBank':[require("开户行")],
          money:[require('付款金额',{trigger:"blur"}),{ validator: decimalTwo, trigger: "blur"}],
          functionary: [require("经手人")],
          timer: [require("付款时间")],
        },
        qiniuImage: {
          id: "qiniuImage", //图片预览的id   
          image_uri: [], //图片上传的列表  只含有url地址
        },
        packJson: {
          maxLength: 50, //最大图片数量
          minLength: 5, //最大图片数量
          accept: "", //image/gif,image/jpeg,image/jpg,image/png如果是这些，可以不传此参数
          size: 4, //图片大小  单位兆
        },
       endDateOpt:{
          disabledDate:(time)=>{
            var timestamp=new Date().getTime()-86400000;       //864代表一天
            return  time.getTime() <  timestamp
          }
      },
      };
    },
    methods: {
      submitForm(formName) {  
        this.$refs[formName].validate(valid => {
          if (valid) {
            var params = {
                    id:this.id?this.id:null,
                    carOrderId: this.carOrderId,
                    payPic: this.buttonValue=='添加' ? this.qiniuImage.image_uri.join(',') : this.tableData.detail.payPics.image_uri.join(',') ,
                    handling:this.buttonValue=='添加' ? this.tableData.functionary : this.tableData.detail.handling,
                    payDate: this.buttonValue=='添加' ? this.changeTime("time",this.tableData.timer):this.changeTime('time',this.tableData.detail.payDate),               
                    bankTransaction: this.buttonValue=='添加' ? this.tableData.number : this.tableData.detail.bankTransaction,
                    payMoney:this.buttonValue=='添加' ? this.tableData.money :this.tableData.detail.payMoney,
                    remark: this.buttonValue=='添加' ? this.tableData.remark : this.tableData.detail.remark,
                    
                  }
              switch(this.buttonValue){
                case '添加' :    
                  agentHttp.post(`adminApi/admin/mortgage_order_remit/add`, params).then(res => {
                    if (res.success) {
                      this.dialogSendData.dialogFormVisible = false;
                      this.dialogSendData.isAdd = false
                      this.$message.success(res.data || "添加成功");
                      this.$emit('Refresh', null);
                    }
                  })
                  break;
                  case 'child编辑' :
                    agentHttp.put(`adminApi/admin/mortgage_order_remit/update`, params).then(res => {
                      if (res.success) {
                        this.dialogSendData.dialogFormVisible = false;
                        this.dialogSendData.isAdd = false
                        this.$message.success(res.data || "添加成功");
                        this.$emit('Refresh', null);
                      }
                    })
                  break;
                  default :
                   
                  break;
              }

          } else {
            console.log("error submit!!");

            return false;
          }
        });
      },
      resetForm(formName) {
        this.dialogSendData.dialogFormVisible = false;
        this.dialogSendData.isAdd = false
      },
      initData() { //初始化数据
        agentHttp.get(`adminApi/admin/mortgage_order_remit/view/${this.carOrderId}/${this.buttonValue=='添加'?0:this.id}`).then((res) => {
          if (res.success) {
            this.tableData = Object.assign({}, res.data, this.tableData);
              //青牛字段
              
            if(this.tableData.detail !==null){
                  this.tableData.detail.payPics = {
                    id:'this.tableData.detail.payPic',
                    image_uri:this.tableData.detail.payPic.split(',').notempty() 
                  }
            }
            
          }
        })
      },
      getDate(time) {
        return timestampToTime(time)
      },
      changeTime(key,time){
        return formatTimer(key,time)
      },
      
    },
    props: {
      dialogSendData: {
        type: Object
      },
      buttonValue: {
        type: String
      },
      carOrderId: {
        type: Number
      },
      id:{
        type:Number
      }
    },
    created() {
      if(this.buttonValue=='添加'){
        this.id = null
      }
      this.initData()
    },
    components: {
      qiniu
    }
  };

</script>

<style lang="scss" scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    margin-top: 20px;
  }

  .align {
    margin: 50px auto 0;
    text-align: center;
  }

</style>

